import { Component, Injector, ViewEncapsulation, ViewChild, ElementRef, AfterViewInit, EventEmitter, Output, Input } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LocationServiceProxy, LocationOfMaterialListDto, AppLicationTreeDto, AppLicationListDto } from '@shared/service-proxies/service-proxies';

@Component({
  selector: 'selectLocation',
  templateUrl: './select-location.component.html',
  encapsulation: ViewEncapsulation.None
})

export class SelectLocationComponent extends AppComponentBase implements AfterViewInit {

  @ViewChild('areaCombobox') areaCombobox: ElementRef;
  @ViewChild('shelfCombobox') shelfCombobox: ElementRef;
  @ViewChild('positionCombobox') positionCombobox: ElementRef;

  _location = "";
  @Output() locationChange = new EventEmitter();

  @Input()
  get location() {
    return this._location;
  }

  set location(val) {
    this._location = val;
    this.locationChange.emit(this._location);
  }

  areas = new Array<AppLicationListDto>();
  areaId = "";
  shelfs = new Array<AppLicationListDto>();
  shelfId = "";
  position = "";
  positions = new Array<AppLicationListDto>();
  constructor(
    injector: Injector,
    private _locationServiceProxy: LocationServiceProxy,
  ) {
    super(injector);
  }

  ngAfterViewInit(): void {
    this._locationServiceProxy.getAreasDrop(undefined, false, 58)
      .subscribe(result => {
        this.areas = result;

      });
  }

  loadShelfs() {
    this.shelfId = "";
    this._location = "";
    console.log(this.areaId)
    this._locationServiceProxy.getAreasDrop(this.areaId, false, 58)
      .subscribe(result => {
        this.shelfs = result;
        console.log(this.shelfs)
      });
  }

  loadPositions() {
    this._location = "";

    this._locationServiceProxy.getAreasDrop(this.shelfId, false, 58)
      .subscribe(result => {
        this.positions = result;
      });
  }

}
